package com.me.waterphase.listener;

import java.util.Iterator;

import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.me.waterphase.WaterArtifact;
import com.me.waterphase.WaterPlayer;
import com.me.waterphase.WaterWorld;
import com.me.waterphase.listener.contact.ArtifactContact;
import com.me.waterphase.listener.contact.ICustomContact;
import com.me.waterphase.utils.PhysicManager.CONTACT_TYPE;

public class ArtifactListener {

	public static void beginContact(Contact contact, boolean _isA) {
		
		//If the player is in contact, give him a reference toward the artifact.
		if(_isA)
		{
			if(contact.getFixtureB().getUserData() != null && ((ICustomContact)contact.getFixtureB().getUserData()).getContactType() == CONTACT_TYPE.USERDATA_PLAYER)
			{
				if(((ArtifactContact) contact.getFixtureA().getUserData()).getNumberContact() == 0)
				{
					//Maximum rope length.
					WaterPlayer player = WaterWorld.getInstance().getPlayer();
					WaterArtifact artifact = ((ArtifactContact) contact.getFixtureA().getUserData()).getArtifact();

					//Set the artifact to the player.
					player.setArtifact(artifact);
					artifact.getBody().setGravityScale(0);
					
					//Remove mass from artifact to prevent player movements.
					for(Iterator<Fixture> it = artifact.getBody().getFixtureList().iterator(); it.hasNext();)
						((Fixture)it.next()).setDensity(0.00001f);
					
					artifact.getBody().resetMassData();
				}

				((ArtifactContact) contact.getFixtureA().getUserData()).increment();
			}
				
		}	
		else
		{
			if(contact.getFixtureA().getUserData() != null && ((ICustomContact)contact.getFixtureA().getUserData()).getContactType() == CONTACT_TYPE.USERDATA_PLAYER)
			{
				if(((ArtifactContact) contact.getFixtureB().getUserData()).getNumberContact() == 0)
				{
					//Maximum rope length.
					WaterPlayer player = WaterWorld.getInstance().getPlayer();
					WaterArtifact artifact = ((ArtifactContact) contact.getFixtureA().getUserData()).getArtifact();
					
					//Set the artifact to the player.
					player.setArtifact(artifact);
					artifact.getBody().setGravityScale(0);
					
					//Remove mass from artifact to prevent player movements.
					for(Iterator<Fixture> it = artifact.getBody().getFixtureList().iterator(); it.hasNext();)
						((Fixture)it.next()).setDensity(0.00001f);
					
					artifact.getBody().resetMassData();
				}
				
				((ArtifactContact) contact.getFixtureB().getUserData()).increment();
			}
		}
	}

	public static void endContact(Contact contact, boolean _isA) {
		
		if(_isA)
		{
			if(contact.getFixtureB().getUserData() != null && ((ICustomContact)contact.getFixtureB().getUserData()).getContactType() == CONTACT_TYPE.USERDATA_PLAYER)
			{
				((ArtifactContact) contact.getFixtureA().getUserData()).decrement();
				
				//If the player no longer has any contact with the artifact, reset gravity and "unhandle" it from the player.
				if(((ArtifactContact) contact.getFixtureA().getUserData()).getNumberContact() == 0)
				{
					WaterArtifact artifact = ((ArtifactContact) contact.getFixtureA().getUserData()).getArtifact();
					
					WaterWorld.getInstance().getPlayer().setArtifact(null);
					artifact.getBody().setGravityScale(1);
					
					//Reset density to the original one.
					for(Iterator<Fixture> it = artifact.getBody().getFixtureList().iterator(); it.hasNext();)
						((Fixture)it.next()).setDensity(artifact.getOriginalDensity());
					
					artifact.getBody().resetMassData();
				}
			}				
		}
		else
		{
			if(contact.getFixtureA().getUserData() != null && ((ICustomContact)contact.getFixtureA().getUserData()).getContactType() == CONTACT_TYPE.USERDATA_PLAYER)
			{
				((ArtifactContact) contact.getFixtureB().getUserData()).decrement();
				
				if(((ArtifactContact) contact.getFixtureA().getUserData()).getNumberContact() == 0)
				{
					WaterArtifact artifact = ((ArtifactContact) contact.getFixtureA().getUserData()).getArtifact();
					
					WaterWorld.getInstance().getPlayer().setArtifact(null);
					artifact.getBody().setGravityScale(1);
					
					//Reset density to the original one.
					for(Iterator<Fixture> it = artifact.getBody().getFixtureList().iterator(); it.hasNext();)
						((Fixture)it.next()).setDensity(artifact.getOriginalDensity());
					
					artifact.getBody().resetMassData();
				}
				
			}
		}
	}

	public static void preSolve(Contact contact, Manifold oldManifold, boolean _isA) {

		if((_isA && contact.getFixtureB().getUserData() != null && ((ICustomContact)contact.getFixtureB().getUserData()).getContactType() == CONTACT_TYPE.USERDATA_PLAYER)
			||(!_isA && contact.getFixtureA().getUserData() != null && ((ICustomContact)contact.getFixtureA().getUserData()).getContactType() == CONTACT_TYPE.USERDATA_PLAYER))
			contact.setEnabled(false);
		
	}

	public static void postSolve(Contact contact, ContactImpulse impulse, boolean _isA) {
		// TODO Auto-generated method stub
	}
}
